package com.fare.taluo.util;

import java.io.File;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;

import com.fare.taluo.vo.auth.UserInfoVO;
import com.fare.taluo.vo.config.city.CityLineSelectVO;
import com.fare.taluo.vo.core.CabinEntityVO;
import com.fare.taluo.vo.core.ChannelPlaneinfoByView;
import com.fare.taluo.vo.core.CplaneInfoByTopFive;
import com.fare.taluo.vo.core.PlaneInfo;
import com.fare.taluo.vo.core.SourceEntityVO;
import com.fare.taluo.vo.core.TransitchangeEntityVO;
import com.fare.taluo.vo.core.UserCareLineEntityVO;
import com.fare.taluo.vo.freight.FreightMonitorSettingsVO;
import com.fare.taluo.vo.report.CsairReportVO;
import com.fare.taluo.vo.single.CabinVO;

public class I18nUtil {

	private static Map<String,String> namesMap = null;
	private static ResourceBundle bundle = null;
	public static Map<String,Map<String,String>> i18nMap=null;
	
	/**
	 * 根据local选择语言
	 * @param locale
	 * @return
	 */
	public static Map<String,String> getProperties(Locale locale){
		
		
		if(null == locale){
			//locale = new Locale("en", "US");
			locale = new Locale("zh", "CN");
		}
		namesMap = getPropertiesNames();
		if(null==i18nMap){
			i18nMap=new HashMap<String,Map<String,String>>();
		}
		Map<String,String> map = new HashMap<String,String>();
		if(null==i18nMap.get(locale.toString())){
			//获得classespath下所有国际化属性文件
			if(null == namesMap){
				namesMap = getPropertiesNames();
			}
			for (String loadPath : namesMap.keySet()) {
				bundle = ResourceBundle.getBundle(loadPath, locale);
				bundle2Map(bundle, map);
			}
			i18nMap.put(locale.toString(), map);
		}
		return i18nMap.get(locale.toString());
	}

	/**
	 * 角色internationalization
	 * 
	*/
	private static Map<String,String> getPropertiesNames(){
		Map<String,String> mTemp = new HashMap<String,String>();
		File f = new File(I18nUtil.class.getResource("/i18n").getPath());
		
		for (File file : f.listFiles()) {
			String path = "i18n"+"."+file.getName();
			File fSecond = new File(file.getPath());
			if(fSecond.isDirectory()){
				for (File fProperty : fSecond.listFiles()) {
					path += "."+fProperty.getName().split("_")[0];
					mTemp.put(path, path);
					//System.out.println(path);
					break;
				}
			}	
		}
		return mTemp;
		
	}
	private static Map<String,String> bundle2Map(ResourceBundle bundle,Map<String,String> map){
		 Enumeration<String> en = bundle.getKeys();
		 while (en.hasMoreElements()) {
			 	String key = en.nextElement();
	            map.put(key, bundle.getString(key));
	        }
		return map;
	}
	
	/**
	 * 国际化航空公司
	 * @param list
	 * @param locale
	 */
	public  static void sourceConvert2I18N(List<SourceEntityVO> list,Locale locale){
		if(null == list || list.isEmpty()){
			return;
		}
		for(SourceEntityVO vo :list){
			vo.setName(null==I18nUtil.getProperties(locale).get(vo.getName())?vo.getName():I18nUtil.getProperties(locale).get(vo.getName()));
			vo.setShortName(null==I18nUtil.getProperties(locale).get(vo.getShortName())?vo.getShortName():I18nUtil.getProperties(locale).get(vo.getShortName()));
		}
	}
	
	public static void companyI18N(List<ChannelPlaneinfoByView> list,Locale locale) {
		if(null == list || list.isEmpty()){
			return;
		}
		if(locale.toString().equalsIgnoreCase("en_US")){
			for(ChannelPlaneinfoByView vo:list){
				vo.setGrabChannel(null==I18nUtil.getProperties(locale).get(vo.getGrabChannel())?vo.getGrabChannel():I18nUtil.getProperties(locale).get(vo.getGrabChannel()));
				vo.setQdName(null==I18nUtil.getProperties(locale).get(vo.getQdName())?vo.getQdName():I18nUtil.getProperties(locale).get(vo.getQdName()));
				
			}
		}
	}

	public static void companyI18N_2(List<CplaneInfoByTopFive> list, Locale locale) {
		if(null == list || list.isEmpty()){
			return;
		}
		if(locale.toString().equalsIgnoreCase("en_US")){
			for(CplaneInfoByTopFive vo:list){
				vo.setChannelName(null==I18nUtil.getProperties(locale).get(vo.getChannelName())?vo.getChannelName():I18nUtil.getProperties(locale).get(vo.getChannelName()));
				vo.setOwName(null==I18nUtil.getProperties(locale).get(vo.getOwName())?vo.getChannelName():I18nUtil.getProperties(locale).get(vo.getOwName()));
			}
		}
		
	}

	
	
	public  static void UserSourceI18N(UserInfoVO vo,Locale locale){
		if(null ==vo ){
			return ;
		}
		vo.setCompany(null==I18nUtil.getProperties(locale).get(vo.getCompany())?vo.getCompany():I18nUtil.getProperties(locale).get(vo.getCompany()));
	}
	
	/**
	 * 舱位国际化
	 * @param list
	 * @param locale
	*/
	public static void cabinI18N(List<CabinVO> list,Locale locale){
		if(null == list || list.isEmpty()){
			return ;
		}
		for(CabinVO vo:list){
			if(locale.toString().equalsIgnoreCase("en_US")){
				vo.setCabinType(null==I18nUtil.getProperties(locale).get(vo.getCabinType())?vo.getCabinType():I18nUtil.getProperties(locale).get(vo.getCabinType()));
			}
		}
	}
	
	/**
	 * 我的关注航线国际化
	 * @param list
	 * @param locale
	*/
	public static void carLineI18N(List<UserCareLineEntityVO>list,Locale locale){
		if(null == list || list.isEmpty()){
			return ;
		}
		for(UserCareLineEntityVO vo:list){
			if(locale.toString().equalsIgnoreCase("en_US")){
				vo.setDepartcity(null==I18nUtil.getProperties(locale).get(vo.getDepartcity())?vo.getDepartcity():I18nUtil.getProperties(locale).get(vo.getDepartcity()));
				vo.setArrivalcity(null==I18nUtil.getProperties(locale).get(vo.getArrivalcity())?vo.getArrivalcity():I18nUtil.getProperties(locale).get(vo.getArrivalcity()));
			}
		}
	}
	
	public static void carLineI18N_2(FreightMonitorSettingsVO vo,Locale locale){
		if(null == vo ){
			return ;
		}
		if(locale.toString().equalsIgnoreCase("en_US")){
			vo.setFromCity(null==I18nUtil.getProperties(locale).get(vo.getFromCity().split("\\(")[0])?vo.getFromCity():I18nUtil.getProperties(locale).get(vo.getFromCity().split("\\(")[0])+"("+vo.getFromCode()+")");
			vo.setToCity(null==I18nUtil.getProperties(locale).get(vo.getToCity().split("\\(")[0])?vo.getToCity():I18nUtil.getProperties(locale).get(vo.getToCity().split("\\(")[0])+"("+vo.getToCode()+")");
		}
	}
	
	public static void carLineI18N_3(List<FreightMonitorSettingsVO> list,Locale locale){
		if(null == list || list.isEmpty()){
			return ;
		}
		
		if(locale.toString().equalsIgnoreCase("en_US")){
			for(FreightMonitorSettingsVO vo:list){
				vo.setFromCity(null==I18nUtil.getProperties(locale).get(vo.getFromCity().split("\\(")[0])?vo.getFromCity():I18nUtil.getProperties(locale).get(vo.getFromCity().split("\\(")[0])+"("+vo.getFromCode()+")");
				vo.setToCity(null==I18nUtil.getProperties(locale).get(vo.getToCity().split("\\(")[0])?vo.getToCity():I18nUtil.getProperties(locale).get(vo.getToCity().split("\\(")[0])+"("+vo.getToCode()+")");
				vo.setAirLine(vo.getFromCity()+"-"+vo.getToCity());
			}
		}
	}
	
	/**
	 * Show Language
	 *  @param locale
	*/
	public static String showLanguage(Locale locale){
		String language="";
		language=locale.toString();
		if(!language.equalsIgnoreCase("zh_CN") && !language.equalsIgnoreCase("en_US")){
			language="other";
		}
		return language;
	}
	
	/**
	 * 热门城市
	 * @param list
	 * @param locale
	*/
	public static void cityCovert2I18N(List<CityLineSelectVO> list ,Locale locale){
		if(null == list || list.isEmpty()){
			return;
		}
		for(CityLineSelectVO vo:list){
			if(locale.toString().equalsIgnoreCase("en_US")){
				vo.getFromCityVO().setName(vo.getFromCityVO().getFullSpell()+"("+vo.getFromCityVO().getCode()+")");
				vo.getToCityVO().setName(vo.getToCityVO().getFullSpell()+"("+vo.getToCityVO().getCode()+")");
			}
		}
	}
	/**
	 * @list citylist
	 * @param locale
	*/
	public static void cityCodeI18N(List<UserCareLineEntityVO>  citylist ,Locale locale){
		for(UserCareLineEntityVO careLine:citylist){
			if(careLine.getArrivalcity().indexOf("(")>0){
				careLine.setArrivalcity(careLine.getArrivalcity().substring(0, careLine.getArrivalcity().indexOf("(")));
			}
			if(careLine.getDepartcity().indexOf("(")>0){
				careLine.setDepartcity(careLine.getDepartcity().substring(0, careLine.getDepartcity().indexOf("(")));
			}
			if(locale.toString().equalsIgnoreCase("en_US")){
				careLine.setArrivalcity(null==I18nUtil.getProperties(locale).get(careLine.getArrivalcity())?careLine.getArrivalcity():careLine.getArrivalcitycode());
				careLine.setDepartcity(null==I18nUtil.getProperties(locale).get(careLine.getDepartcity())?careLine.getDepartcity():careLine.getDepartcitycode());
			}
			
		}
	}
	/**
	 * monitor
	 * @list citylist
	 * @param locale
	*/
	public static void cityMonitorI18N(List<FreightMonitorSettingsVO>  citylist ,Locale locale){
		if(citylist ==null || citylist.isEmpty()){
			return ;
		}
		if(locale.toString().equalsIgnoreCase("en_US")){
			for(FreightMonitorSettingsVO vo:citylist){
				vo.setFromCity(vo.getFromCode());
				vo.setToCity(vo.getToCode());
				vo.setAirLine(vo.getFromCode()+"-"+vo.getToCode());
			}
		}
		
	}
	/**
	 * 航线价差
	 *  @list voList
	 * @param locale
	*/
	public static void queryDataI18N(List<CsairReportVO> voList, Locale locale) {
		if(voList==null || voList.isEmpty()){
			return ;
		}
		if(locale.toString().equalsIgnoreCase("en_US")){
			for(CsairReportVO vo:voList){
				vo.setFromCityName(null==I18nUtil.getProperties(locale).get(vo.getFromCityName())?vo.getFromCityName():I18nUtil.getProperties(locale).get(vo.getFromCityName()));
				vo.setToCityName(null==I18nUtil.getProperties(locale).get(vo.getToCityName())?vo.getToCityName():I18nUtil.getProperties(locale).get(vo.getToCityName()));
				vo.setQunaerAgentName(null==I18nUtil.getProperties(locale).get(vo.getQunaerAgentName())?vo.getQunaerAgentName():I18nUtil.getProperties(locale).get(vo.getQunaerAgentName()));
			}
		}
		
	}
	
	public static void planeInfoConvert2I18N(List<PlaneInfo> list,Locale locale){
		if(null == list || list.isEmpty()){
			return;
		}
		if(locale.toString().equalsIgnoreCase("en_US")){
			for(PlaneInfo vo :list){
				vo.setFromCityName(I18nUtil.getProperties(locale).get(
						vo.getFromCityName()) == null ? vo.getFromCityName()
						: I18nUtil.getProperties(locale).get(vo.getFromCityName()));
				vo.setToCityName(I18nUtil.getProperties(locale).get(
						vo.getToCityName()) == null ? vo.getToCityName()
						: I18nUtil.getProperties(locale).get(vo.getToCityName()));
				vo.setGrabChannel(I18nUtil.getProperties(locale).get(
						vo.getGrabChannel()) == null ? vo.getGrabChannel()
						: I18nUtil.getProperties(locale).get(vo.getGrabChannel()));
				vo.setCarrierFullName(I18nUtil.getProperties(locale).get(
						getCarrierFullNameMap().get(vo.getCarrierKey())) == null ? vo.getGrabChannel()
						: I18nUtil.getProperties(locale).get(getCarrierFullNameMap().get(vo.getCarrierKey())));
				vo.setCarrierName(I18nUtil.getProperties(locale).get(
						getCarrierNameMap().get(vo.getCarrierKey())) == null ? vo.getGrabChannel()
						: I18nUtil.getProperties(locale).get(getCarrierNameMap().get(vo.getCarrierKey())));
				if("en_US".equals(locale.toString()) && null!=vo.getAgentName()){
					vo.setAgentName(Pinying4jUtil.trans2QuanPin(vo.getAgentName()));
				}
			   if(null != vo.getTransit() && vo.getTransit().size()>0){
				   for(TransitchangeEntityVO transit :vo.getTransit() ){
					   if(null!=transit.getFlightNo()){
						   transit.setCarrierName(I18nUtil.getProperties(locale).get(
									getCarrierNameMap().get(transit.getFlightNo().substring(0,2))) == null ? vo.getGrabChannel()
											: I18nUtil.getProperties(locale).get(getCarrierNameMap().get(transit.getFlightNo().substring(0,2))));
						   transit.setCarrierFullName(I18nUtil.getProperties(locale).get(
									getCarrierFullNameMap().get(transit.getFlightNo().substring(0,2))) == null ? vo.getGrabChannel()
											: I18nUtil.getProperties(locale).get(getCarrierFullNameMap().get(transit.getFlightNo().substring(0,2))));
						   String fromAirPortName=transit.getFromAirPortName();
						   String fromName=fromAirPortName;
						   String fromPort=null;
						   if(null!=fromAirPortName && fromAirPortName.indexOf("(")>0){
								   fromName=fromAirPortName.substring(0, fromAirPortName.indexOf("(")).replace(" ", "");
								   fromPort=transit.getFromAirPortName().substring(transit.getFromAirPortName().indexOf("(")); 
							   }
							   if(null!=I18nUtil.getProperties(locale).get(fromName)){
								  fromAirPortName=I18nUtil.getProperties(locale).get(fromName)+fromPort;
						   }
								   
							   
						   String toAirPortName=transit.getToAirPortName();
						   String toName=toAirPortName;
						   String toport=null;
						    if(null!=toAirPortName && toAirPortName.indexOf("(")>0){
							   toName=toAirPortName.substring(0, toAirPortName.indexOf("(")).replace(" ", "");
							   toport=transit.getToAirPortName().substring(transit.getToAirPortName().indexOf("(")); 
						    }
							if(null!=I18nUtil.getProperties(locale).get(toName)){
								
								toAirPortName=I18nUtil.getProperties(locale).get(toName)+toport;
							}
							   
						    
						   transit.setFromAirPortName(fromAirPortName);
						   transit.setToAirPortName(toAirPortName);
					   }
				   }
			   }
			   if(null !=vo.getCabins() && vo.getCabins().size()>0){
				   for(CabinEntityVO cabvo:vo.getCabins()){
					   cabvo.setCabinName(I18nUtil.getProperties(locale).get(cabvo.getCabinName()));
					   cabvo.setCabinType(I18nUtil.getProperties(locale).get(cabvo.getCabinType()));
				   }
			   }
			   if(null!=vo.getReturnPlaneInfo() && vo.getReturnPlaneInfo().size()>0){
				   planeInfoConvert2I18N(vo.getReturnPlaneInfo(),locale);
			   }
			}
		}
	}
	
	private static Map<String,String> getCarrierFullNameMap(){
		Map<String, String> homeMap = new HashMap<String, String>();
		homeMap.put("CA", "中国国际航空");
		homeMap.put("CZ", "中国南方航空");
		homeMap.put("MU", "中国东方航空");
		homeMap.put("ZH", "深圳航空");
		homeMap.put("HU", "海南航空");
		homeMap.put("3U", "四川航空");
		homeMap.put("MF", "厦门航空");
		homeMap.put("SC", "山东航空");
		homeMap.put("DZ", "东海航空");
		homeMap.put("QW", "青岛航空");
		homeMap.put("DR", "瑞丽航空");
		homeMap.put("JD", "首都航空");
		homeMap.put("KY", "昆明航空");
		homeMap.put("GS", "天津航空");
		homeMap.put("8L", "祥鹏航空");
		homeMap.put("PN", "西部航空");
		homeMap.put("BK", "奥凯航空");
		homeMap.put("HO", "吉祥航空");
		return homeMap;
	}
	
	public static Map<String,String> getCarrierNameMap(){
		Map<String,String> map =new HashMap<String,String>();
		map.put("CA", "国航");
		map.put("CZ", "南航");
		map.put("MU", "东航");
		map.put("ZH", "深航");
		map.put("HU", "海航");
		map.put("3U", "川航");
		map.put("MF", "厦航");
		map.put("SC", "山航");
		map.put("DZ", "东海");
		map.put("QW", "青岛");
		map.put("DR", "瑞丽");
		map.put("JD", "首都");
		map.put("KY", "昆明");
		map.put("GS", "天津");
		map.put("8L", "祥鹏");
		map.put("PN", "西部");
		map.put("BK", "奥凯");
		map.put("HO", "吉祥");
		return map;	
	}


}
